<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <!-- 最新版本的 Bootstrap 核心 CSS 文件 -->
    <link rel="stylesheet" href="https://cdn.bootcss.com/bootstrap/3.3.7/css/bootstrap.min.css"
          integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">

    <script src="http://cdn.bootcss.com/jquery/3.1.1/jquery.min.js"></script><!-- 部分content的里面可能用到jQuery，所以前置了 -->

</head>
<body>


<textarea id="context" style="width: 1000px; height: 263px;">
CREATE TABLE `bee` (
  `id` int(11) NOT NULL AUTO_INCREMENT COMMENT 'id',
  `age` int(11) DEFAULT '0' COMMENT '年龄',
  `name` varchar(200) DEFAULT NULL COMMENT '名字',
  `description` varchar(500) DEFAULT NULL COMMENT '描述',
  `expire` tinyint(1) NOT NULL DEFAULT '0' COMMENT '是否会过期',
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8
</textarea>

<BR><BR>

<div class="panel panel-success">
    <div class="panel-heading">Dao代码部分</div>
    <div id="myDivDao" class="panel-body" style="color: #0096ff;">
        public interface Dao extends MyMapper<<span id="myClsName">Bee</span>> { <BR>
        &nbsp;&nbsp;&nbsp;&nbsp;Dao $ = mapper(Dao.class);<BR>
        } <BR>
    </div>
</div>

<div class="panel panel-warning">
    <div class="panel-heading">java bean 属性</div>

    <div id="myDiv" class="panel-body" style="color: #0096ff;">
        复制表结构到上面的textarea, 然后回车!~
    </div>
</div>


<script type="text/javascript">

    $(function () {

        function trim(s) {
            return s.replace(/(^\s*)|(\s*$)/g, "");
        }

        var dontCreate = new Array();
        dontCreate.push("create");
        dontCreate.push("drop");
        dontCreate.push("alter");

        var myDiv = $("#myDiv");
        var context = $("#context");

        function toArrays(line) {
            var obj = {};

            var s = trim(line).split(" ");

            var hasComment = false;
            var arr = new Array();
            for (var j = 0; j < s.length; j++) {
                if (s[j] == " " || s[j] == "") {
                    continue;
                }

                if (s[j].toLowerCase() == "comment") {
                    hasComment = true;
                }

                arr.push(s[j]);
            }

            obj.hasComment = hasComment;
            obj.arr = arr;
            return obj;
        }

        context.keydown(function (e) {
            if (e.keyCode != 13) {
                return;
            }

            myDiv.html("");

            var str = context.val();

            var strs = str.split("\n");

            var myHtml = '';
            var theCreateAmount = 0;
            for (var i = 0; i < strs.length; i++) {

                var sLower = trim(strs[i].toLowerCase());

                if (!sLower || sLower.length == 0) {
                    continue;
                }

                var firstStr = sLower[0];

                if (firstStr.indexOf("(") != -1 || firstStr.indexOf("/") != -1) {
                    continue;
                }

                // 类名
                if (sLower.indexOf("create") != -1 && theCreateAmount == 0) {
                    var daoObj = toArrays(sLower);
                    var daoArr = daoObj.arr;
                    var beanName = daoArr[2];
                    beanName = beanName.replace(/`/g, "");

                    var tfn = beanName.split("_");
                    beanName = '';
                    for (var x = 0; x < tfn.length; x++) {
                        var xiaoName = tfn[x];
                        if (xiaoName.indexOf("tb") != -1) {
                            continue;
                        }
                        var temp = xiaoName[0].toUpperCase();
                        beanName += temp;
                        temp = xiaoName.substr(1);
                        beanName += temp;
                    }

                    $('#myClsName').html(beanName);
                }

                // 过滤这些打头的文件
                var theBack = false;
                for (var j = 0; j < dontCreate.length; j++) {
                    if (sLower.indexOf(dontCreate[j]) != -1) {
                        theBack = true;
                        break;
                    }
                }

                if (theBack) {
                    if (sLower.indexOf("create") != -1) {
                        if (theCreateAmount == 0) {
                            theCreateAmount++;
                            continue
                        }
                    } else {
                        continue;
                    }
                }

                if (sLower.indexOf("primary") != -1) {
//                    console.info("over !");
                    break;
                }

                var obj = toArrays(strs[i]);
                var arr = obj.arr;
                var hasComment = obj.hasComment;

                if (arr.length < 3) {
                    continue;
                }

                // 字段名
                var fieldName = arr[0].replace(/`/g, "");

                // 字段注释
                var fieldComment = '';
                if (hasComment) {
                    var strLine = strs[i];
                    var commentIndex = strLine.indexOf("comment");
                    if (commentIndex != -1) {
                        strLine = strLine.substr(commentIndex + 8);
                        fieldComment = strLine.replace(/'/g, "").replace(/,/, "");
                    }
                }

                // 给成员属性添加数据类型
                var fieldType = arr[1].toLowerCase();

                var codeFiledType = '';

                // byte short int long char boolean

                // int(11) varchar

                if (fieldType.indexOf("varchar") != -1) {
                    codeFiledType = "String"
                }

                if (fieldType.indexOf("int") != -1) {
                    codeFiledType = "Integer";

                    if (fieldName == "id") {
                        codeFiledType = "int";
                        fieldComment = '';
                    }
                }

                if (fieldType.indexOf("tinyint") != -1) {
                    codeFiledType = "Byte";
                }

                if (fieldType.indexOf("timestamp") != -1) {
                    codeFiledType = "Date";
                }

                if (fieldType.indexOf("smallint") != -1) {
                    codeFiledType = "Short";
                }

                if (fieldType.indexOf("bigint") != -1) {
                    codeFiledType = "Long";
                }

                if (fieldType.indexOf("float") != -1) {
                    codeFiledType = "Float";
                }

                if (fieldType.indexOf("bool") != -1) {
                    codeFiledType = "Boolean";
                }

                if (fieldType.indexOf("date") != -1) {
                    codeFiledType = "Date";
                }

                if (fieldType.indexOf("timestamp") != -1) {
                    codeFiledType = "Timestamp";
                }

                if (fieldType.indexOf("tinyint(1") != -1) {
                    codeFiledType = "Boolean";

                    if (fieldName.indexOf("is_") != -1) {
                        fieldName = fieldName.replace(/is_/, "");
                    }
                }

                if (fieldComment.length != 0) {
                    myHtml += "/** " + fieldComment + " */<BR>";
                }

                myHtml += codeFiledType + " ";
                myHtml += fieldName + ";<BR>";

//                console.info(fieldComment, fieldName);

            }
//            console.info(myHtml);
            myDiv.html(myHtml);

        });

        function selectText(element) {
            var text = document.getElementById(element);
            if (document.body.createTextRange) {
                var range = document.body.createTextRange();
                range.moveToElementText(text);
                range.select();
            } else if (window.getSelection) {
                var selection = window.getSelection();
                var range = document.createRange();
                range.selectNodeContents(text);
                selection.removeAllRanges();
                selection.addRange(range);
            } else {
                alert("none");
            }
        }

        myDiv.click(function () {
            selectText("myDiv");
        });

        $('#myDivDao').click(function () {
            selectText('myDivDao');
        })

    });
</script>


</body>
</html>
